import java.util.TreeMap;
import java.util.Map.Entry;
import java.util.Iterator;

import java.util.SortedMap;

public class TreeMapRewrite{
    public static void main(String[] args){
        TreeMap<Key, String> tMap = new TreeMap<Key, String>();
        tMap.put(new Key(1), "aaa");
        tMap.put(new Key(2), "bbb");
        tMap.put(new Key(3), "ccc");
        tMap.put(new Key(1), "ddd");
        tMap.put(new Key(2), "eee");
        
        System.out.println("size: " + tMap.size());
        System.out.println("tMap: ");
        System.out.println(tMap);
        
        Iterator<Entry<Key, String>> iter = tMap.entrySet().iterator();
        while(iter.hasNext()){
            Entry<Key, String> en = iter.next();
            System.out.println(en.getKey() + " : " + en.getValue());
        }
        
        System.out.println("first key: " + tMap.firstKey());
        System.out.println("last key: " + tMap.lastKey());
        
        SortedMap<Key, String> sMap = tMap.headMap(new Key(2));
        System.out.println(sMap);
        
        SortedMap<Key, String> sMap2 = tMap.tailMap(new Key(2));
        System.out.println(sMap2);
    }
}

class Key implements Comparable<Key>{
    int id;
    
    public Key(int id){
        this.id = id;
    }
    
    public int compareTo(Key k){
        return this.id - k.id;
    }
    
    public String toString(){
        return "[ id = " + id + " ]";
    }
}